import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { ChunlianModule } from "../../../modules/ChunlianModule";
import { ItemData } from "../resource/info/ItemData";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { ConfigCenter } from "../resource/ConfigCenter";
import { ConfigConst } from "../resource/configBase/ConfigConst";
import { TGuessCouplet } from "../../../Message/Db/Tables/TGuessCouplet";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { IChunlianModule } from "../../../modules/interfaces/IChunlianModule";
type int = number;
//class ChunlianController
    
    export  class ChunlianController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (!this._chunlianModule) 
            {
                this._chunlianModule = new ChunlianModule();
                this._chunlianModule.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onAddedToStageHandler);
                this._chunlianModule.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onRemovedFromStageHandler);
                this.updateRewards();
            }
            return this._chunlianModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this._chunlianModule.reset();
            return;
        }

        /* internal  */onRemovedFromStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this._isNeedRequest = false;
            return;
        }

        /* internal  */updateRewards(): void
        {
            var loc4=null;
            var loc1="14186001#14011959#14094410#14094411#13009507#13009505#13400102#14037105#14043501";
            var loc2=loc1.split("#");
            var loc3=[];
            var loc5=0;
            while (loc5 < loc2.length) 
            {
                loc4 = new ItemData(int(loc2[loc5]));
                loc3.push(loc4);
                ++loc5;
            }
            this._chunlianModule.updateRewards(loc3);
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.ChunlianOpen, this.onChunlianOpenHandler);
            Dispatcher.addEventListener(EventName.ChunlianOpenSuccess, this.onChunlianOpenSuccessHandler);
            Dispatcher.addEventListener(EventName.ChunlianAnswer, this.onChunlianAnswerHandler);
            Dispatcher.addEventListener(EventName.ChunlianAnswerSuccess, this.onChunlianAnswerSuccessHandler);
            return;
        }

        /* internal  */onChunlianOpenHandler(arg1: DataEvent): void
        {
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc1=arg1.data as ItemData;
            if (this._itemData == null || this._itemData && !(this._itemData.uid == loc1.uid)) 
            {
                this._itemAmount = loc1.itemAmount;
            }
            this._itemData = loc1;
            if (loc1 && loc1.itemExInfo) 
            {
                loc2 = loc1.itemExInfo.couplet_code;
                loc3 = loc1.itemExInfo.couplet_chs;
            }
            if (!this._isNeedRequest && loc3 && !(loc3 == "")) 
            {
                if (loc4 = ConfigCenter.getConfigs(ConfigConst.TGuessCouplet, ["code"], [loc2], true) as TGuessCouplet) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ChunlianOpenSuccess, {"upperCouplet": loc4.upperCouplet, "chs": loc3}));
                }
            }
            else 
            {
                GameProxy.packProxy.initOpenGuessCouplet(loc1.uid);
            }
            return;
        }

        /* internal  */onChunlianOpenSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["upperCouplet"];
            var loc2=arg1.data["chs"];
            view.show();
            this._chunlianModule.reset();
            this._chunlianModule.itemData = this._itemData;
            this._chunlianModule.updateChs(loc2);
            this._chunlianModule.updateUper(loc1);
            return;
        }

        /* internal  */onChunlianAnswerHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["lowerCouple"] as string;
            GameProxy.packProxy.answerGuessCouplet(loc1, loc2);
            return;
        }

        /* internal  */onChunlianAnswerSuccessHandler(arg1: DataEvent): void
        {
            var loc2=0;
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                loc2 = 58003;
            }
            else 
            {
                loc2 = 58004;
            }
            MsgManager.showRollTipsMsg(Language.getString(loc2));
            var loc3;
            var loc4=((loc3 = this)._itemAmount - 1);
            loc3._itemAmount = loc4;
            if (this._itemAmount != 0) 
            {
                this._isNeedRequest = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChunlianOpen, this._itemData));
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */splitStirng(arg1: string): Array<any>
        {
            var loc1=[];
            var loc2=0;
            while (loc2 < arg1.length) 
            {
                loc1.push(arg1.substr(loc2, 1));
                ++loc2;
            }
            return loc1;
        }

        private /* var */_chunlianModule: IChunlianModule;

        private /* var */_itemData: ItemData;

        private /* var */_itemAmount: int;

        private /* var */_isNeedRequest: Boolean;
    }
